package cn.rengy.web.framework.shiro.config;

import java.util.Map;

import javax.servlet.Filter;

import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.spring.web.config.ShiroFilterChainDefinition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import cn.rengy.web.framework.shiro.filter.authc.StatelessAuthenticationFilter;



  
  
@Configuration  
public class ShiroWebFilterConfiguration {
	
	@Value("${shiro.loginUrl:/login}")
    protected String loginUrl;
	
    @Value("${shiro.successUrl:/loginSuccess}")
    protected String successUrl;

    @Value("${shiro.unauthorizedUrl:/error/404.html}")
    protected String unauthorizedUrl;
    @Autowired
    protected ShiroFilterChainDefinition shiroFilterChainDefinition;
    @Autowired
    protected SecurityManager securityManager;
    
    
    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean() {
    	ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();  
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        //过滤器配置和过滤器链规则配置只是初始化参数，实际生效是靠FilterChainManager管理的
        //初始化自定义的过滤器有顺序的，默认的过滤器将在FactoryBean中初始化
        Map<String, Filter> filters=shiroFilterFactoryBean.getFilters();
        //filters.put("authen", new IAuthenticationFilter());//认证 
        filters.put("jwtLogin", new StatelessAuthenticationFilter());
        //默认访问 /login.jsp
        shiroFilterFactoryBean.setLoginUrl(loginUrl);
        //登录成功后要跳转的连接
        shiroFilterFactoryBean.setSuccessUrl(successUrl);
        //用户访问未对其授权的资源时，所显示的连接
        shiroFilterFactoryBean.setUnauthorizedUrl(unauthorizedUrl);
        
        shiroFilterFactoryBean.setFilterChainDefinitionMap(shiroFilterChainDefinition.getFilterChainMap());
        
        return shiroFilterFactoryBean;
    }
    
    
    
}  